package com._21cn.cms.article.utils;

import com._21cn.cms.article.entity.vo.ListArticle;
import com._21cn.framework.utils.StringUtil;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Thinkpad on 2017/3/30.
 */
public class ArticleUtil {

    /**
     * 格式化摘要
     *
     * @param htmlContent
     * @param length
     * @return
     */
    public static String formatSummary(String htmlContent, int length) {
        String summary = "";
        if (StringUtil.isEmpty(htmlContent))
            return summary;
        summary = htmlContent.replaceAll("<img[^>]*?/>", "").replaceAll("#CONTENTSPLITPAGE#", "")
                .replaceAll("<p class=\"pager\">.*</p>", "").replaceAll("<[a-zA-Z]+[1-9]?[^><]*>", "")
                .replaceAll("</[a-zA-Z]+[1-9]?>", "").replaceAll("&ldquo;", "").replaceAll("&nbsp;", "")
                .replace("&middot;", "").replaceAll("&rdquo;", "").replaceAll("&rsquo;", "").replaceAll("&lsquo;", "")
                .replaceAll("\\r\\n|\\n|\\r|\\t", "").replaceAll(" ", "").replaceAll("　", "")
                .replaceAll("<!--.*-->", "").replaceAll("&quot;", "“").replaceAll("\"", "“").replaceAll("'", "‘")
                .replaceAll("<", "&lt;").replace(">", "&gt;");
        if (summary.length() > length) {
            summary = summary.substring(0, length) +
                    "...";
        } else {
            summary = summary +
                    "...";

        }
        return summary;
    }

    public static List<Integer> getArticleIdList(List<? extends ListArticle> result) {
        List idList = new ArrayList();
        for (ListArticle obj : result) {
            if ((obj != null) && (obj.getArticleId().intValue() > 0)) {
                idList.add(obj.getArticleId());
            }
        }
        return idList;
    }
}
